import 'package:flutter/material.dart';
import 'package:get/get_rx/src/rx_typedefs/rx_typedefs.dart';
import 'package:nft_shop/market/page/market_list_page.dart';
import 'package:nft_shop/my/model/models.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/custom_underline_tabIndicator.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/status_view.dart';

import '../conteroller/market_conteroller.dart';

class MarketPage extends StatefulWidget {
  final VoidCallback? onPressed;
  final Function(int index) onPressedSearch;

  MarketPage({Key? key, this.onPressed, required this.onPressedSearch})
      : super(key: key);

  @override
  State<MarketPage> createState() => _MarketPageState();
}

class _MarketPageState extends State<MarketPage>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  TabController? _tabController;

  MarketConteroller _conteroller = MarketConteroller.to;

  final PageController _pageController = PageController(initialPage: 0);

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getConfigNameDetail("two_sale_market");
  }
  ConfigNameDetail? article;

  @override
  Widget build(BuildContext context) {
    print("市场刷新--");
    if (_tabController == null) {
      _tabController = TabController(vsync: this, length: 2);
    }
    return Scaffold(
        appBar: AppBar(
          title: titleWidget(),
          leading: Container(
            padding: const EdgeInsets.only(right: 12, top: 12, bottom: 12),
            child: SizedBox(
              width: 36,
            ),
          ),
          actions: [
            IconButton(
              icon: LoadAssetImage(
                "market_screen",
                width: 15,
                height: 15,
              ),
              onPressed: widget.onPressed,
            ),
            IconButton(
              icon: LoadAssetImage(
                "market_search",
                width: 15,
                height: 15,
              ),
              onPressed: () {
                widget.onPressedSearch(_tabController!.index);
              },
            ),
          ],
        ),
        body:
        PageView(
          key: const Key('pageView'),
          onPageChanged: (int index) {
            _conteroller.currentIndex = index;
            _tabController!.animateTo(index);
            if(index==1){
              getConfigNameDetail("two_sale_market");
            }
          },
          controller: _pageController,
          children: [
            MarketListPage(
              index: 0,
            ),
           getSaleState()
          ],
        ));
  }
  void getConfigNameDetail(String name) async {
    try {
      List list = await ServiceRepository.getConfigNameDetail(name);
      list.forEach((element) {
        if (element.name.toString().trim() == name) {
          article = element;
        }
      });
    } catch (e, s) {}
    setState(() {});
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _pageController.dispose();
  }

  //title
  Widget titleWidget() {
    return Theme(
      data: Theme.of(context).copyWith(
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
      ),
      child: Center(
        child: TabBar(
          isScrollable: true,
          controller: _tabController,
          labelStyle:
              TextStyle(fontSize: 18, fontFamily: Global.mySemiboldFont),
          unselectedLabelStyle:
              TextStyle(fontSize: 16, fontFamily: Global.mySemiboldFont),
          indicatorPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 5),
          indicatorSize: TabBarIndicatorSize.label,
          labelPadding: EdgeInsets.only(
            left: 14,
            right: 14,
          ),
          unselectedLabelColor: Global.kTheme.textPrimary3,
          labelColor: Global.kTheme.textPrimary1,
          indicator: CustomUnderlineTabIndicator(
              borderSide: BorderSide(
            color: Global.kTheme.textPrimary4,
            width: 3,
          )),
          tabs: const <Widget>[
            Tab(text: "首发"),
            Tab(text: "转售"),
          ],
          onTap: (index) {
            if (!mounted) {
              return;
            }
            _conteroller.currentIndex = index;
            _pageController.jumpToPage(index);
          },
        ),
      ),
    );
  }

  //切换控制器
  void _onPageChange(int index) {
    _tabController!.animateTo(index);
  }

  getSaleState() {
    if(article?.value=="true"){
      return  MarketListPage(
        index: 1,
      );
    }else{
      return Center(
        child: StatusViews(LoadStatusType.empty,emptyText: "二级市场等待开放，敬请期待",),
      );
    }
  }
}
